package experiment_01_Pram;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;

public class PhoneBookManagement {
	private static Scanner scanner = new Scanner(System.in);
	static Map<String, String> map = new HashMap<String, String>();

	public static void main(String[] args) {
		while (true) {
			System.out.println("**欢迎进入电话簿管理系统**");
			System.out.println("1.增加");
			System.out.println("2.删除");
			System.out.println("3.查找");
			System.out.println("4.修改");
			System.out.println("5.查看");
			System.out.println("0.退出");
			System.out.println("请选择你想要进行的操作：");
			int choice = scanner.nextInt();
			if (choice == 1 || choice == 2 || choice == 3 || choice == 4 || choice == 5 || choice == 0) {
				switch (choice) {
				case 1:
					add();
					break;
				case 2:
					delect();
					break;
				case 3:
					find();
					break;
				case 4:
					update();
					break;
				case 5:
					display();
					break;
				case 0:
					return;
				}
				System.out.println("\n");
			} else {
				System.out.println("输入不合法，请重新输入！");
			}
		}
	}

	private static void add() {
		System.out.print("请输入用户的电话号码：");
		String tel = scanner.next();
		System.out.print("请输入用户的名字：");
		String name = scanner.next();
		if (map.isEmpty()) {
			map.put(name, tel);
			System.out.println("添加成功！");
		} else {
			boolean flag = true;
			for (Map.Entry<String, String> entry : map.entrySet()) {
				if (entry.getKey().equals(name)) {
					flag = false;
				}
			}
			if (flag) {
				map.put(name, tel);
				System.out.println("添加成功！");
			} else {
				System.out.println("电话簿中已经存在这个电话，无法添加！");
			}
		}
	}

	private static void delect() {
		System.out.print("请输入用户名字：");
		String name = scanner.next();
		if (map.isEmpty()) {
			System.out.println("电话簿为空，无法进行删除。");
		} else {
			boolean flag = true;
			for (Map.Entry<String, String> entry : map.entrySet()) {
				if (entry.getKey().equals(name)) {
					flag = false;
					map.remove(entry.getKey());
					map.remove(entry.getValue());
				}
			}
			if (!flag) {
				System.out.println("删除用户信息成功!");
			} else {
				System.out.println("电话簿中不存在此用户信息，无法进行删 除!");
			}
		}

	}

	private static void find() {
		System.out.print("请输入用户的姓名：");
		String name = scanner.next();
		if (map.isEmpty()) {
			System.out.println("电话簿为空！");
		} else {
			boolean flag = true;
			for (Map.Entry<String, String> entry : map.entrySet()) {
				if (entry.getKey().equals(name)) {
					System.out.println("用户姓名：" + entry.getKey() + "\t\t 用户电话：" + entry.getValue());
					flag = false;
				}
			}
			if (!flag) {
			} else {
				System.out.println("查询失败!");
			}
		}

	}

	private static void update() {
		System.out.print("请输入用户的姓名：");
		String name = scanner.next();
		if (map.isEmpty()) {
			System.out.println("电话簿为空！");
		} else {
			boolean flag = true;
			for (Map.Entry<String, String> entry : map.entrySet()) {
				if (entry.getKey().equals(name)) {
					System.out.print("请输入用户的电话：");
					String tel = scanner.next();
					map.put(name, tel);
					flag = false;
				}
			}
			if (!flag) {
			} else {
				System.out.println("修改失败!");
			}
		}

	}

	private static void display() {
		if (map.isEmpty()) {
			System.out.println("电话簿为空！");
		} else {
			Set<Map.Entry<String, String>> entrySet = map.entrySet();
			for (Map.Entry<String, String> entry : entrySet) {
				System.out.println(entry.getKey() + " " + entry.getValue());
			}
		}
	}

}
